<!--资源-->
<template>
  <PPMPage
    v-if="pageData.length > 0"
    :formData="formData"
    :pageData="pageData"
    @on-sumbit="submitFun"
    @goback="closeFun"
  />
</template>
<script>
import ppmmixin from "@/mixins/ppmmixin.js";
import ppmTaskInit from "@/mixins/custorder/ppmTaskInit";
import CustOrderService from "@/api/order/CustOrderService";
import { mapState } from "vuex";
import requestUtil from "@/utils/requestEnc";
import custOrderDetail from "@/mixins/custorder/custOrderDetail";
import OrderService from "@/api/order/OrderService.js";
export default {
  name: "ChangePPM",
  mixins: [ppmTaskInit, ppmmixin, custOrderDetail],
  data() {
    return {
      taskReqDate: "",
      result: [],
      formLabelWidth: "120px",
      fullscreenLoading: false,
      newCTOInfo: [],
      orderTaskId: "",
      orderTask: "",
      orderTasks: {},
    };
  },
  computed: {
    ...mapState("orders", ["orderPosInfo"]),
    ...mapState("user", ["user", "busiManagerId", "custManagerId"]),
  },
  props: {
    ODKInfo: {
      type: Object,
      default: () => {
        return {};
      },
    },
    CTOInfo: {
      type: [Array, Object],
    },
  },
  created() {},
  async mounted() {
    // console.log("CTOInfos:", this.CTOInfos);
    // console.log("ODKInfo:", this.ODKInfo);
    if (this.CTOInfos instanceof Array) {
      this.newCTOInfo = this.CTOInfos;
    } else {
      this.newCTOInfo = [this.CTOInfos];
    }
    if (this.newCTOInfo.length > 0) {
      var orderItemIds = "";
      for (var i = 0; i < this.newCTOInfo.length; i++) {
        if (i == 0) {
          orderItemIds += this.newCTOInfo[i].orderItemId;
        } else {
          orderItemIds += "," + this.newCTOInfo[i].orderItemId;
        }
      }

      this.orderItemIds = orderItemIds;

      var scopeInfos = [];
      scopeInfos.push({ scope: "orderTasks" });
      var custSoNumber = this.CTOInfos[0].custSoNumber;
      var batchNbr = this.CTOInfos[0].batchNbr;
      var custId = this.CTOInfos[0].custId;

      const result = await CustOrderService.qryCustomerOrderDetail({
        custSoNumber,
        batchNbr,
        orderItemIds,
        custId,
        scopeInfos,
      });
      var orderTasks = result.orderDetails[0]["orderTasks"];

      for (var i = 0; i < orderTasks.length; i++) {
        if (orderTasks[i].taskReqDate && !this.taskReqDate) {
          this.taskReqDate = this.$util.dateFormat(
            orderTasks[i].taskReqDate,
            "yyyy-MM-dd"
          );
        }
        //资源填报
        if (orderTasks[i].taskId == this.ODKInfo.taskId) {
          var orderTaskIds = orderTasks[i];
          break;
        }
      }

      this.orderTaskId = orderTaskIds.orderTaskId;

      localStorage.setItem(
        "cacheInfo",
        JSON.stringify({
          taskId: this.ODKInfo.taskId,
          prodOfferId: this.newCTOInfo[0].prodOfferId,
          nodeName: orderTaskIds.nodeName,
          taskReqDate: orderTaskIds.taskReqDate,
          staffName: this.user.staffName,
          productId: this.CTOInfos[0].prodId,
          linkTel: orderTaskIds.linkTel,
          //需求单类型/审核日期
          reqType: this.newCTOInfo[0].reqType,
          reqAudDate: this.newCTOInfo[0].reqAudDate,
        })
      );
    }
  },
  watch: {},
  methods: {
    async submitFun() {
      //debugger;
      let base = {
        custSoNumber: this.newCTOInfo[0].custSoNumber, //流水号  从上个页面带回
        custId: this.newCTOInfo[0].custId,
        orderItemIds: this.orderItemIds,
        taskId: this.ODKInfo.taskId,
        orderAuditId: this.newCTOInfo[0].orderAuditId,
        oneOrmany: "one",
        //orderTaskId: this.orderTaskId,
        userId: "__STAFF_ID__",
        orderItemSeq: this.newCTOInfo[0].orderItemSeq,
      };

      let param = requestUtil.paramEnc(this.formData, base);

      const result = await CustOrderService.saveResourceVerification({
        ...base,
        ...param,
      });
      try {
        if (result.meta.resultCode == 0) {
          this.$message({
            type: "success",
            message: "保存成功!",
          });
          this.$emit("update:show", false);
        } else {
          this.$message({
            showClose: true,
            message: result.meta.resultMsg,
            type: "error",
          });
        }
      } catch {
        this.$message({
          showClose: true,
          message: "code:" + result.code + "  msg:" + result.msg,
          type: "error",
        });
      }
    },
    closeFun() {
      this.$emit("close");
    },
  },
};
</script>
<style scoped>
</style>


